/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sistemadeproyectos;
import java.util.Calendar;
import java.util.Date;
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.text.JTextComponent;
import java.text.DateFormatSymbols;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;
import org.jfree.data.time.SimpleTimePeriod;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class Cronograma extends ApplicationFrame{
   
    protected JButton btn1;
    protected JButton btn2;
    static String equipo;
    
    Cronograma(final String title, String equipo) {
        super(title);
        final IntervalCategoryDataset dataset = createDataset();
        final JFreeChart pizarra = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(pizarra);
        btn1 = new JButton("Inicio");
        btn2 = new JButton("Fin");
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 350));
        setContentPane(chartPanel);
        JPanel pnlBottom = new JPanel();
        getContentPane().add(pnlBottom, "South");
        pnlBottom.add(btn1);
        pnlBottom.add(btn2);
        //pack();
        //RefineryUtilities.centerFrameOnScreen(this);
        //this.setVisible(true);
        //  getContentPane().add(pnlBottom, "East");
    }
    /**
     * Se crean los elementos que tendra el diagrama.
     */
    public static IntervalCategoryDataset createDataset() {
        
        final TaskSeries s1 = new TaskSeries("Probando");
        
        BDAdmin ver = new BDAdmin("SELECT * FROM PROYECTO");
        ResultSet rs = ver.consulta();
        
        boolean x = true;
        boolean y = false;
        String actividad;
        Date fechaini;
        Date fechafin;
        String nombre;
        int i = 1;
        int j = 0;
        System.out.println("ACA1");
        try {	
         if (rs.next()){
          while (x){   
            System.out.println("ACA2");  
            fechaini = rs.getDate("FECHA_INICIO");
            fechafin = rs.getDate("FECHA_FIN");
            nombre = rs.getString("NOMBRE");
            actividad = rs.getString("ACTIVIDAD");
            
            String year1= fechaini.toString();
            System.out.println("year1 es: "+year1);
            year1 = year1.substring(0,4);
            System.out.println("YEAR1 es: "+year1);
            int anio1 = Integer.parseInt(year1);
            System.out.println("anio1 es: "+anio1);
            
            String month1= fechaini.toString();
            month1 = month1.substring(5,7);
            int mes1 = Integer.parseInt(month1)-1;
            System.out.println("mes1 es: "+mes1);
            
            String day1= fechaini.toString();
            day1 = day1.substring(8,10);
            int dia1 = Integer.parseInt(day1);
            System.out.println("dia1 es: "+dia1);
            
            String year2= fechafin.toString();
            year2 = year2.substring(0,4);
            int anio2 = Integer.parseInt(year2);
            System.out.println("anio2 es: "+anio2);
            
            String month2= fechafin.toString();
            month2 = month2.substring(5,7);
            int mes2 = Integer.parseInt(month2)-1;
            System.out.println("mes2 es: "+mes2);
            
            String day2= fechafin.toString();
            day2 = day2.substring(8,10);
            int dia2 = Integer.parseInt(day2);
            System.out.println("dia2 es: "+dia2);
            
            s1.add(new Task(actividad,
               new SimpleTimePeriod(date(dia1, mes1, anio1),
                                    date(dia2, mes2, anio2))));
               x = rs.next();
              }               
             }
            }
	     catch (Exception e) {
	     e.printStackTrace();
            }
        
         ver.cerrar();
        //final TaskSeries s2 = new TaskSeries("Actual");
        final TaskSeriesCollection collection = new TaskSeriesCollection();
        collection.add(s1);
       
     //   collection.add(s2);
        return collection;
    }
    /**
     * metodo que crea objetos Date.
     */
    private static Date date(final int dia, final int mes, final int anio) {
        final Calendar calendar = Calendar.getInstance();
        calendar.set(anio, mes, dia);
        final Date result = calendar.getTime();
        return result;
    }
        
    /**
     * Se crea una pizarra
     */
    private JFreeChart createChart(final IntervalCategoryDataset dataset) {
        final JFreeChart chart = ChartFactory.createGanttChart(
            "Cronograma:",  // titulo
            "Actividades",              // label de dominio
            "        Fecha",              // label de rango
            dataset,             
            true,                // leyenda
            true,                // tooltips
            false                // urls
        );
    //    btn1 = new JButton("Inicio");
//        chart.getCategoryPlot().getDomainAxis().setMaxCategoryLabelWidthRatio(10.0f);
        return chart;    
   }
    
    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(final String[] args) {
        final Cronograma cron = new Cronograma("Cronograma", equipo);
        cron.pack();
        RefineryUtilities.centerFrameOnScreen(cron);
        RefineryUtilities.createJButton("PRUEBA", null);
        cron.setVisible(true);
    }
}